package com.example.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.Configuration;
import com.example.bean.File;
import com.example.bean.Order;
import com.example.printerfile.Printer;
import com.example.utils.HttpUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.swing.text.html.parser.Entity;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * 订单处理类
 */
public class OrderHandler {

    /**
     * 根据订单ID查询订单信息
     * @param orderId
     */
    public void getOrderInfo(String orderId){

        String httpEntity = HttpUtils.get(Configuration.getOrderInfoUri(orderId));
        if(httpEntity == null){
            System.out.println("订单信息获取失败，请检查服务端是否正常在线！");
            return;
        }
        boolean result = HttpUtils.checkRsponse(httpEntity);
        if(!result){
            System.out.println("订单信息获取失败,订单查询失败！");
            return;
        }
        JSONObject jsonObject = HttpUtils.parseResponse(httpEntity);
        if(jsonObject == null){
            return;
        }
        String data = jsonObject.getString("data");
        //解析为Order对象
        Order order  = JSON.parseObject(data,Order.class);
        //查看结果
        String orderStr = order.toString();
        System.out.println("订单信息:"+orderStr);
        //修改订单状态
        this.modifyOrderStatus(orderId,1);
        //处理文件
        this.fileHandler(orderId,order.getFiles());
    }

    /**
     * 处理file 下载文件
     * @param files 从后端请求到的文件数组
     */
    public void fileHandler(String orderId,ArrayList<File> files){
        Iterator<File> iterator = files.iterator();
        while (iterator.hasNext()){
            File file = iterator.next();
            //获取文件下载路径
            String filePath  = HttpUtils.download(Configuration.SERVER_HOST+file.getViewPath(),"D:/download",file.getFileName());
            //将文件提交到打印机队列
            try {
                boolean result = Printer.handlerFile(filePath,"HP LaserJet Pro MFP M125-M126 PCLmS");
                if(result){
                    this.modifyOrderStatus(orderId,2);
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    /**
     * 修改订单 状态
     * @param orderId 订单编号
     * @param status 修改后的订单状态
     */
    public void modifyOrderStatus(String orderId,int status){
        try{
           String httpEntity = HttpUtils.get(Configuration.modifyOrderUri(orderId,status));
            if(httpEntity == null){
                System.out.println("订单状态修改失败！orderId:"+orderId);
                return;
            }
            boolean result = HttpUtils.checkRsponse(httpEntity);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
